var Working = false;// 判断是否画图的状态
var shape = null;// 图形对象
var shapePath = "" // 图形对象轨迹
var xx = 0, yy = 0;// 记录点击后的鼠标位置
var tempX = 0, tempY = 0;// 已经存在轨迹点坐标
var oldX = 0;
oldY = 0;// 原始坐标点
var trakPoint = true;// 是否被标示为轨迹点
var divStr = "";// 每个轨迹点div
var posNum = 0;// 每个坐标点的ID值
var importFlag = true;
var leftID = "";
var domainStr = "";
var reSetFlag = false;// 重新绘制标示
// 鼠标单击事件
var getEvent = function(evt) {
	return evt ? evt : (window.event ? window.event : null);
};
function divMousedown(ev) {
	reSetFlag = false;
	var event = getEvent(ev);
	var srcElement = event.srcElement || event.target;
	if (event.button == 1 && srcElement.tagName != "HTML"
			&& srcElement.tagName != "BODY") {

		if (!Working)// 第一次点击鼠标
		{
			posNum++;
			xx = event.x + document.documentElement.scrollLeft;
			yy = event.y + document.documentElement.scrollTop;
			shape = getId("mainDiv")
					.appendChild(
							document
									.createElement("<v:shape id='polyNew' path='m 0,0 l 0,0' filled='false' style='cursor:pointer;position:absolute;z-index:10;left:"
											+ xx
											+ "px;top:"
											+ yy
											+ "px;width:100px;height:100px;filter: Alpha(Opacity=60);' strokecolor='green' strokeweight='2px' coordsize='100,100'/>"));
			shapePath = shape.path.value.replace("e", "");
			oldx = xx;
			oldy = yy;
			Working = true;
		} else {

			if (shape != null) {
				shapePath = shape.path.value.replace(" e", "");
			} else {
				if (srcElement.id != "") {
					var len = divStr.lastIndexOf(srcElement.id);
					if (len != (divStr.length - srcElement.id.length)) {
						return false;
					}
					shape = getId("polyNew");
					shapePath = shape.path.value.replace(" e", "");
					reSetFlag = true;
				} else {
					return false;
				}
			}
			oldx = event.x + document.documentElement.scrollLeft;
			oldy = event.y + document.documentElement.scrollTop;
			posNum++;
		}
		if (importFlag) {
			var currentID = srcElement.id;
			if (trakPoint) {
				getId("mainDiv")
						.appendChild(
								document
										.createElement("<div id='divPos"
												+ posNum
												+ "' title='"
												+ (oldx)
												+ ","
												+ (oldy)
												+ "' style='position:absolute;left:"
												+ (oldx - 3)
												+ "px;top:"
												+ (oldy - 3)
												+ "px;width:6px;height:6px;overflow:hidden;border:1px solid red;z-index:210;background:red;cursor:pointer;'  onmouseover='oldPosMouseMove(this)' onmouseout='oldPosMouseOut(this)' oncontextmenu='subMenu(this);return false;'></div>"));
				if (divStr == "") {
					divStr = "divPos" + posNum;
				} else {
					divStr = divStr + ",divPos" + posNum;
				}
			}
			if (currentID.indexOf("read") == 0) {
				if (domainStr == "") {
					domainStr = "divPos" + posNum + "," + currentID + ":"
							+ (oldx + "," + oldy);
				} else {
					domainStr = domainStr + ";divPos" + posNum + ","
							+ currentID + ":" + (oldx + "," + oldy);
				}
			} else {
				if (!reSetFlag) {
					if (domainStr == "") {
						if (trakPoint) {
							domainStr = "divPos" + posNum + ":"
									+ (oldx + "," + oldy);
						} else {
							domainStr = currentID + ":" + (oldx + "," + oldy);
						}
					} else {
						if (trakPoint) {
							domainStr = domainStr + ";divPos" + posNum + ":"
									+ (oldx + "," + oldy);
						} else {
							domainStr = domainStr + ";" + currentID + ":"
									+ (oldx + "," + oldy);
						}
					}
				}
			}
			getId("path").value = domainStr;
		}
	}
}
function subMenu(obj) {
	var popu = getId("popu");
	popu.style.display = "block";
	leftID = obj.id;
	popu.style.top = parseInt(obj.style.top) + "px";
	popu.style.left = parseInt(obj.style.left) + 10 + "px";
	return false;
}
// 鼠标移动的时候
function divMouseMove() {
	// 鼠标当前的坐标位置
	tempx = event.x + document.documentElement.scrollLeft;
	tempy = event.y + document.documentElement.scrollTop;
	if (shape != null) {
		if (trakPoint) {
			shape.path.value = shapePath + "," + (tempx - xx) + ","
					+ (tempy - yy);
		} else {
			shape.path.value = shapePath + "," + (tempX - xx) + ","
					+ (tempY - yy);
		}
		var tempPath = shape.path.value;
		if (tempPath.indexOf(",0 e") >= 0 && posNum > 2) {
			importFlag = false;
		} else {
			importFlag = true;
		}
		shape.path.value = shape.path.value.replace(",0,0,", ",0,").replace(
				",0 e", "e");
	}
}

function divMouseUp() {
	if (event.button == 2) {
		if (shape) {
			shape.path.value = shapePath + "e";
			shape = null;
			trakPoint = false;
		}
	}
}
function oldPosMouseMove(obj) {
	trakPoint = false;
	var pos = obj.title;
	if (pos) {
		pos = pos.split(",");
		tempX = pos[0];
		tempY = pos[1];
	}
	obj.style.background = "blue";
	obj.style.border = "1px solid blue";
}
function oldPosMouseOut(obj) {
	trakPoint = true;
	obj.style.background = "red";
	obj.style.border = "1px solid red";
}
document.onmousedown = divMousedown;
document.onmousemove = divMouseMove;
document.onmouseup = divMouseUp;
function delePos() {
	if (divStr != "") {
		var saveDivStr = divStr.substring(0, divStr.indexOf(leftID) - 1);
		var delDivStr = divStr.substring(divStr.indexOf(leftID));
		domainStr = domainStr.substring(0, domainStr.indexOf(leftID) - 1);
		getId("path").value = domainStr;
		var delDivArr = delDivStr.split(",");
		var divID = getId(leftID);
		var posXY = divID.title;
		for ( var i = 0; i < delDivArr.length; i++) {
			getId("mainDiv").removeChild(document.getElementById(delDivArr[i]));
		}
		var shape = document.getElementById("polyNew");
		var posXYArr = posXY.split(",");
		var tempXY = (posXYArr[0] - xx) + "," + (posXYArr[1] - yy);
		var path = shape.path.value;
		path = path.substring(0, path.indexOf(tempXY));
		shape.path.value = path + " e";
		divStr = saveDivStr;
		if (divStr == "") {
			getId("mainDiv").removeChild(document.getElementById("polyNew"));
			Working = false;
			posNum = 0;
		}
	}
	popuDisplay();
}
function popuDisplay() {
	var popu = getId("popu");
	popu.style.display = "none";
}
function overIn(obj) {
	obj.className = "overIn";
}
function overOut(obj) {
	obj.className = "overOut";
}
function parePaht() {
	var editPath = getId("path").value;
	var shape;
	var shapePath = "m0,0 l0,0";
	if (editPath != "") {
		Working = true;
		var editPos = editPath.split(";");
		for ( var pos = 0; pos < editPos.length; pos++) {
			var coord = editPos[pos].split(":");
			var coordXY = coord[1].split(",");
			var divID, xPos, yPos;
			if (coord[0].indexOf(",") >= 0) {
				divID = coord[0].substring(0, coord[0].indexOf(","));
			}else{
				divID=coord[0];
			}
			posNum = parseInt(divID.replace("divPos", ""));
			xPos = parseInt(coordXY[0]);
			yPos = parseInt(coordXY[1]);
			if (pos == 0) {
				getId("mainDiv")
						.appendChild(
								document
										.createElement("<v:shape id='polyNew' path='m 0,0 l 0,0' filled='false' style='cursor:pointer;POSITION:absolute;z-index:10;left:"
												+ xPos
												+ "px;top:"
												+ yPos
												+ "px;width:100px;height:100px;filter: Alpha(Opacity=60);' strokecolor='green' strokeweight='2px' coordsize='100,100'/>"));
				shape = getId("polyNew");
				xx = xPos;
				yy = yPos;
			}
			var shapePath = shapePath + "," + (xPos - xx) + "," + (yPos - yy);
			getId("mainDiv")
					.appendChild(
							document
									.createElement("<div id='"
											+ divID
											+ "' title='"
											+ (xPos)
											+ ","
											+ (yPos)
											+ "' style='position:absolute;left:"
											+ (xPos - 3)
											+ "px;top:"
											+ (yPos - 3)
											+ "px;width:6px;height:6px;overflow:hidden;border:1px solid red;z-index:210;background:red;cursor:pointer;'  onmouseover='oldPosMouseMove(this)' onmouseout='oldPosMouseOut(this)' oncontextmenu='subMenu(this);return false;'></div>"));
			if (divStr == "") {
				divStr = divID;
			} else {
				divStr = divStr + "," + divID;
			}
			if (domainStr == "") {
				domainStr = coord[0] + ":" + (xPos + "," + yPos);
			} else {
				domainStr = domainStr + ";" + coord[0] + ":"
						+ (xPos + "," + yPos);
			}

		}
		shape.path.value = shapePath;
		shape.path.value = shape.path.value.replace(",0,0,", ",0,").replace(
				",0 e", "e");
	}
}